package csu.webcourse.mypetstore.web.servlet;

import csu.webcourse.mypetstore.domain.Account;
import csu.webcourse.mypetstore.domain.Product;
import csu.webcourse.mypetstore.service.CategoryService;
import csu.webcourse.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class SearchProductsServlet extends HttpServlet {
    private static final String SEARCH_PRODUCTS_FORM = "/WEB-INF/jsp/catalog/searchProducts.jsp";
    private static final String ERROR_FORM = "/WEB-INF/jsp/common/error.jsp";
    private String errorMsg;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String keyword = req.getParameter("keyword");
        HttpSession session = req.getSession();
        CategoryService categoryService = new CategoryService();

        if (keyword == null || keyword.equals("") || keyword.length() < 1) {
            this.errorMsg = "Please enter a keyword to search for, then press the search button.";
            session.setAttribute("errorMsg", this.errorMsg);

            Account account = (Account) session.getAttribute("loginAccount");
            if (account != null) {
                HttpServletRequest httpRequest = req;
                String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                LogService logService = new LogService();
                //最后加入的信息“XXXXX”应当为该界面的信息以及一些商品信息
                String logInfo = logService.logInfo(" ") + strBackUrl + "用户输入搜索关键词有误";
                logService.insertLogInfo(account.getUsername(), logInfo);
            }

            req.getRequestDispatcher(ERROR_FORM).forward(req, resp);
        } else {
            List<Product> productList = categoryService.searchProductList(keyword.toLowerCase());
            session.setAttribute("productList", productList);

            Account account = (Account) session.getAttribute("loginAccount");
            if (account != null) {
                HttpServletRequest httpRequest = req;
                String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                LogService logService = new LogService();
                //最后加入的信息“XXXXX”应当为该界面的信息以及一些商品信息
                String logInfo = logService.logInfo(" ") + strBackUrl + "用户查询" + keyword + "成功";
                logService.insertLogInfo(account.getUsername(), logInfo);
            }

            req.getRequestDispatcher(SEARCH_PRODUCTS_FORM).forward(req, resp);
        }
    }
}
